package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.vo.R;
import com.itheima.dto.sysConfig.Config;
import com.itheima.dto.sysConfig.Config2;
import com.itheima.dto.sysConfig.Config3;
import com.itheima.entity.SysConfig;
import com.itheima.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangpei
 * @ClassName SysconfigController
 * @date 2022/5/29/18:56
 */
@RequestMapping("/sys_config")
@RestController
@Slf4j
public class SysConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping("/all")
    private R<Config> all() {
        //config
        LambdaQueryWrapper<SysConfig> sysConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysConfigLambdaQueryWrapper.groupBy(SysConfig::getTabValue);
        List<SysConfig> sysConfigs = configService.list(sysConfigLambdaQueryWrapper);
        Config config = new Config();
        //config.list
        List<Config2> config2List = new ArrayList<>();
        config2List.add(new Config2("基础配置", "base"));
        config2List.add(new Config2("其他设置", "custom"));
        config2List.add(new Config2("系统设置", "system"));
        config2List.stream().map(item -> {
            item.getKey();
            LambdaQueryWrapper<SysConfig> sysConfigLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            sysConfigLambdaQueryWrapper1.eq(SysConfig::getTabValue, item.getKey());
            sysConfigLambdaQueryWrapper1.groupBy(SysConfig::getGroupValue);
            List<SysConfig> sysConfigs1 = configService.list(sysConfigLambdaQueryWrapper1);
            List<Config3> config3s = sysConfigs1.stream().map(item2 -> {
                Config3 config3 = new Config3();
                config3.setName(item2.getGroupValue());
                LambdaQueryWrapper<SysConfig> sysConfigLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
                sysConfigLambdaQueryWrapper2.eq(SysConfig::getGroupValue, item2.getGroupValue());
                config3.setData(configService.list(sysConfigLambdaQueryWrapper2));
                return config3;
            }).collect(Collectors.toList());
            item.setChildren(config3s);
            return item;
        }).collect(Collectors.toList());
        config.setList(config2List);
        //config.allData
        List<SysConfig> sysConfigs1 = configService.list();
        for (int i = 0; i < sysConfigs1.size(); i++) {
            if (StringUtils.isNotEmpty(sysConfigs1.get(i).getConfigValue().toString())) {
                String[] split = sysConfigs1.get(i).getConfigValue().toString().split("&");
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put("label", split[0].split("=")[0]);
                hashMap.put("value", split[0].split("=")[1]);
                HashMap<Object, Object> hashMap2 = new HashMap<>();
                hashMap2.put("label", split[1].split("=")[0]);
                hashMap2.put("value", split[1].split("=")[1]);
                ArrayList<HashMap> list = new ArrayList<>();
                list.add(hashMap);
                list.add(hashMap2);
                sysConfigs1.get(i).setConfigValue(list);
            } else {
                sysConfigs1.get(i).setConfigValue("");
            }
        }
        config.setAllData(sysConfigs1);

        //config.configForm
        List<String> keys = configService.get();
        Map<String, String> configForm = new HashMap<>();
        for (String key : keys) {
            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StringUtils.isNotEmpty(key), SysConfig::getAttrKey, key);
            SysConfig one = configService.getOne(wrapper);
            String attrValue = one.getAttrValue();
            configForm.put(key, attrValue);
        }
        config.setConfigForm(configForm);

        //config.configRules
        Map<String, String> rulesMap = new HashMap<>();
        List<String> keys1 = configService.get();
        for (String key : keys) {
            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StringUtils.isNotEmpty(key), SysConfig::getAttrKey, key);
            SysConfig one = configService.getOne(wrapper);
            if (StringUtils.isNotEmpty(key) || StringUtils.isNotEmpty(one.getValidator())) {
                rulesMap.put(key, one.getValidator());
            }
        }
        config.setConfigRules(rulesMap);
        return R.success(config);
    }


    @PostMapping("/update")
    public R<String> update(@RequestBody List<SysConfig> configList) {
        configService.updateBatchById(configList);
        return R.success(null);
    }
}
